package com.shop.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.feign.SeckillFeign;
import com.shop.mapper.Pms_SkuInfo_Mapper;
import com.shop.pojo.pms.Pms_Sku_Images;
import com.shop.pojo.pms.Pms_Sku_Info;
import com.shop.pojo.pms.Pms_Spu_Info;
import com.shop.pojo.pms.Pms_Spu_Info_Desc;
import com.shop.service.*;
import com.shop.utils.LayuiUtils;
import com.shop.utils.R;
import com.shop.vo.SeckillRedisVo;
import com.shop.vo.SkuItemSaleAttrVo;
import com.shop.vo.SkuItemVo;
import com.shop.vo.SpuItemAttrGroupVo;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class Pms_SkuInfo_ServiceImpl extends ServiceImpl<Pms_SkuInfo_Mapper, Pms_Sku_Info> implements Pms_SkuInfo_Service {
    @Autowired
    private Pms_Spu_Desc_Service pms_spu_desc_service;
    @Autowired
    private Pms_Sku_Images_Service pms_sku_images_service;
    @Autowired
    private Pms_AttrOrAttrGroup_Service pms_attrOrAttrGroup_service;
    @Autowired
    private Pms_Sku_Sale_Value_Service pms_sku_sale_value_service;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private Pms_Spu_Info_Service pms_spu_info_service;
    @Autowired
    private Pms_SkuInfo_Mapper pms_skuInfo_mapper;
    @Autowired
    private SeckillFeign seckillFeign;


    public SkuItemVo item( Long skuId) {

        SkuItemVo skuItemVo = new SkuItemVo();
        //等到所有任务都完成
        try {
            long start=System.currentTimeMillis();//测试开始时间

            //supplyAsync需要返回结果  因为345依赖1
            CompletableFuture<Pms_Sku_Info> infoCompletableFuture = CompletableFuture.supplyAsync(() -> {
                //1、获取sku的基本信息
                Pms_Sku_Info pms_sku_info = getById(skuId);
                skuItemVo.setInfo(pms_sku_info);
                return pms_sku_info;
            }, threadPoolExecutor);

            CompletableFuture<Void> saleFuture = infoCompletableFuture.thenAcceptAsync((res) -> {
                //3、获取spu的销售属性组合
                List<SkuItemSaleAttrVo> saleAttrsBySpuId = pms_sku_sale_value_service.getSaleAttrsBySpuId(res.getSpu_id());
                skuItemVo.setSaleAttrs(saleAttrsBySpuId);
            }, threadPoolExecutor);
            CompletableFuture<Void> descFuture = infoCompletableFuture.thenAcceptAsync((res) -> {
                //4、获取spu的介绍

                Pms_Spu_Info_Desc byId = pms_spu_desc_service.getById(res.getSpu_id());
                skuItemVo.setDesp(byId);

            }, threadPoolExecutor);
            CompletableFuture<Void> baseAttrFuture = infoCompletableFuture.thenAcceptAsync((res) -> {
                //5、获取spu的规格参数信息
                List<SpuItemAttrGroupVo> attrGroupWithAttrsBySpuId = pms_attrOrAttrGroup_service.getAttrGroupWithAttrsBySpuId(res.getCatalog_id(), res.getSpu_id());
             System.out.println("---------------"+attrGroupWithAttrsBySpuId);
                skuItemVo.setAttrGroups(attrGroupWithAttrsBySpuId);
            }, threadPoolExecutor);
            CompletableFuture<Void> baseSkuByBrandId = infoCompletableFuture.thenAcceptAsync((res) -> {
                //6、根据品牌来查询当前品牌下的商品
                List<Pms_Sku_Info> allSkuinfoByBrandId = pms_skuInfo_mapper.findAllSkuinfoByBrandId(res.getBrand_id(),res.getSku_id());
                skuItemVo.setBrandBySkuInfo(allSkuinfoByBrandId);
            }, threadPoolExecutor);
            CompletableFuture<Void> baseSkuByCatalogId = infoCompletableFuture.thenAcceptAsync((res) -> {
                //7、//根据分类来查询当前分类下的商品
                List<Pms_Sku_Info> allSkuinfoByCatalog = pms_skuInfo_mapper.findAllSkuinfoByCatalog(res.getCatalog_id(),res.getSku_id());
                skuItemVo.setCatalogBySkuInfo(allSkuinfoByCatalog);
            }, threadPoolExecutor);

            CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
                //获取sku的图片信息
                List<Pms_Sku_Images> allSkuImgById = pms_sku_images_service.findAllSkuImgById(skuId);
                skuItemVo.setImages(allSkuImgById);
            }, threadPoolExecutor);
            //获取sku秒杀信息
            CompletableFuture<Void> seckill = CompletableFuture.runAsync(() -> {
                R seckillBySkuId = seckillFeign.getSeckillBySkuId(skuId);
                if (seckillBySkuId.getCode() == 0) {
                    SeckillRedisVo data = seckillBySkuId.getData(new TypeReference<SeckillRedisVo>() {
                    });
                    if (data != null) {
                        skuItemVo.setSeckillRedisVo(data);
                    }
                }
            }, threadPoolExecutor);

            CompletableFuture.allOf(saleFuture,descFuture,baseAttrFuture,baseSkuByBrandId,baseSkuByCatalogId,imageFuture,seckill).get();
            long endTime =System.currentTimeMillis();//测试结束时间
            System.out.println("程序运行时间"+(endTime-start));

        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        return skuItemVo;
    }

    @Override
    public void saveSkuInfo(Pms_Sku_Info pms_sku_info) {
        this.baseMapper.insert(pms_sku_info);
    }

    /*
     * @Description 根据品牌、分类、sku名字、价格区间来查询sku信息
     * @param page 分页页码
     * @param pms_sku_info
     * @param betWeenPrice  价格
     * @return com.shop.utils.LayuiUtils<com.shop.pojo.pms.Pms_Sku_Info>
     * @author liusupeng
     * @date 2020/11/9 15:32
     */
    @Override
    public LayuiUtils<Pms_Sku_Info> findAllSkuQuery(Integer page, Pms_Sku_Info pms_sku_info,String betWeenPrice) {
        String[] split=new String[1];
        //区分价格区间,前台传来的区间必须是 0-100  1-200 这种类型的
        if(betWeenPrice!=null&&!betWeenPrice.equals("")){
            split  = betWeenPrice.split("-");
        }


        LayuiUtils<Pms_Sku_Info> pms_sku_infoLayuiUtils=new LayuiUtils<>();
            QueryWrapper<Pms_Sku_Info> pms_sku_infoQueryWrapper=new QueryWrapper<>();
            if(pms_sku_info.getSku_name()!=null&&!pms_sku_info.getSku_name().equals("")){
                pms_sku_infoQueryWrapper.like("sku_name",pms_sku_info.getSku_name());
            }
            if(pms_sku_info.getBrand_id()!=null&&pms_sku_info.getBrand_id()>0){
                pms_sku_infoQueryWrapper.eq("brand_id",pms_sku_info.getBrand_id());
            }
            if(pms_sku_info.getCatalog_id()!=null&&pms_sku_info.getCatalog_id()>0){
                pms_sku_infoQueryWrapper.eq("catalog_id",pms_sku_info.getCatalog_id());
            }
            if(betWeenPrice!=null&&!betWeenPrice.equals("")){
                pms_sku_infoQueryWrapper.between("price",split[0],split[1]);
            }
            Page<Pms_Sku_Info> pms_sku_infoPage=new Page<>(page,10);
        Page<Pms_Sku_Info> pagesku = pms_skuInfo_mapper.selectPage(pms_sku_infoPage, pms_sku_infoQueryWrapper);
        pms_sku_infoLayuiUtils.setData(pagesku.getRecords());
        pms_sku_infoLayuiUtils.setCount(pagesku.getTotal());

        return pms_sku_infoLayuiUtils;
    }

    @Override
    public R findAllSkuInfoBySkuId(Long skuId) {

        QueryWrapper<Pms_Sku_Info> pms_sku_infoQueryWrapper=new QueryWrapper<Pms_Sku_Info>();
        pms_sku_infoQueryWrapper.eq("sku_id",skuId);
        R r1=new R();
        r1.data(pms_skuInfo_mapper.selectOne(pms_sku_infoQueryWrapper));

        return r1;
    }

    /**
     * 根据商品的sku_Id查询出该商品信息
     * @param skuId
     * @return
     */
    @Override
    public Pms_Sku_Info get_web_SkuInfo_ByID(Long skuId) {

        QueryWrapper<Pms_Sku_Info>  query = new QueryWrapper<>();
        query.eq("sku_id",skuId);
        return pms_skuInfo_mapper.selectOne(query);
    }

    /**
     * 根据商品的ID查询商品的最新价格
     * @param sku_id
     * @return
     */
    @Override
    public BigDecimal getZuiXinPrice(long sku_id) {
        Pms_Sku_Info pms_sku_info = pms_skuInfo_mapper.selectById(sku_id);
        return pms_sku_info.getPrice();
    @Override
    public R exitSkuInfo(Pms_Sku_Info pms_sku_info) {
        int i = pms_skuInfo_mapper.updateById(pms_sku_info);
        if(i>0){
            return R.ok();
        }else{
            return R.error(500,"服务器异常");
        }

    }
    /*
     * @Description 随机查询三条数据进行查询
     * @param
     * @return java.util.List<com.shop.pojo.pms.Pms_Sku_Info>
     * @author liusupeng
     * @date 2020/12/4 8:32
     */
    @Override
    public List<Pms_Sku_Info> getQueryByThree() {

        return pms_skuInfo_mapper.getQueryByThree();
    }


}
